package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.Company;
import com.clkj.xiuzheng.pojo.User;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.*;
import com.clkj.xiuzheng.service.shangcheng.ScUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/scUser")
@CrossOrigin
@Api(value = "/scUser", tags = {"Z商城用户管理"})
public class ScUserController {
    @Autowired
    private ScUserService scUserService;

    @ApiOperation(value = "用户添加收获地址")
    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody Address address) {
        try {
            scUserService.addAddress(address);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "根据(公司id<可选>,用户名称<可选>,开始时间<可选>,结束时间<可选>)查询用户集合")
    @PostMapping("/getUserList")
    public Result getUserList(@ApiParam(name = "companyId", value = "公司id")@RequestParam(required = false) Integer companyId,
                              @ApiParam(name = "userName", value = "用户名称")@RequestParam(required = false) String userName,
                              @ApiParam(name = "startTime", value = "开始时间")@RequestParam(required = false) String startTime,
                              @ApiParam(name = "endTime", value = "结束时间")@RequestParam(required = false) String endTime,
                              @RequestParam Integer pageSize,
                              @RequestParam Integer pageNum) {
        try {
            PageInfo list =scUserService.getUserList(companyId,pageSize,pageNum,userName,startTime,endTime);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "根据(公司id<可选>,用户名称<可选>,开始时间<可选>,结束时间<可选>)查询操作员集合")
    @PostMapping("/getManagerList")
    public Result getManagerList(@RequestParam(required = false) Integer companyId,
                              @RequestParam(required = false) String userName,
                              @RequestParam(required = false) String startTime,
                              @RequestParam(required = false) String endTime,
                              @RequestParam Integer pageSize,
                              @RequestParam Integer pageNum) {
        try {
            PageInfo list =scUserService.getManagerList(companyId,pageSize,pageNum,userName,startTime,endTime);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("新增身份分配菜单以及新增身份后再分配菜单")
    @PostMapping("/addIdentity")
    public Result addIdentity(@RequestBody Identity identity){
        try {
            scUserService.addIdentity(identity);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation("根据公司id<可选>获取获取身份集合")
    @PostMapping("/getIdentityByCompanyId")
    public Result getIdentityByCompanyId(@ApiParam(name = "companyId", value = "公司id")@RequestParam(required = false) Integer companyId){
        try {
            List<Identity> list =scUserService.getIdentityByCompanyId(companyId);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("根据身份id移除菜单权限")
    @PostMapping("/removeIdentityMenuById")
    public Result removeIdentityMenuById(@ApiParam(name = "IndentityInfoIds", value = "身份id字符转 , ")String IndentityInfoIds){
        try {
            scUserService.removeIdentityMenuById(IndentityInfoIds);
            return new Result(true, StatusCode.OK,"移除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"移除失败");
        }
    }
    @ApiOperation("根据身份id获取菜单权限集合")
    @PostMapping("/getIdentityMenuById")
    public Result getIdentityMenuById(@ApiParam(name = "identityId", value = "身份id")Integer identityId){
        try {
            List<Identity> list =scUserService.getIdentityMenuById(identityId);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("根据获取菜单id获取菜单集合")
    @PostMapping("/getMenuList")
    public Result getMenuList(){
        try {
            List<Menu> list =scUserService.getMenuList();
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("获取推广公司id")
    @PostMapping("/getCompanyList")
    public Result getCompanyList(){
        try {
            List<ProductCompany> list =scUserService.getCompanyList();
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("获取推广员集合")
    @PostMapping("/getpromoterList")
    public Result getpromoterList(@ApiParam(name = "userName", value = "推广员名称")@RequestParam(required = false) String userName,
                                  @ApiParam(name = "companyId", value = "1查询所有，否则查询改推广公司的推广员") @RequestParam(required = false) Integer  companyId,
                              @RequestParam Integer pageSize,
                              @RequestParam Integer pageNum) {
        try {
            PageInfo list =scUserService.getpromoterList(userName,companyId,pageSize,pageNum);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("添加给推广员发放佣金记录")
    @PostMapping("/addCommission")
    public Result addCommission(@ApiParam(name = "userId", value = "推广员Id")@RequestParam(required = false) Integer  userId,
                                  @ApiParam(name = "grantMoney", value = "发放的金额") @RequestParam(required = false) Double  grantMoney,
                                @ApiParam(name = "note", value = "备注") @RequestParam(required = false) String  note) {
        try {
            scUserService.addCommission(userId,grantMoney,note);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("获取推广员发放佣金记录")
    @PostMapping("/listCommission")
    public Result listCommission(@ApiParam(name = "userId", value = "推广员Id")@RequestParam(required = false) Integer  userId) {
        try {
            List<UserCommDetail> detailList = scUserService.listCommission(userId);
            return new Result(true, StatusCode.OK,"操作成功",detailList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("成为推广员审核")
    @PostMapping("/applyPromoter")
    public Result applyPromoter(@ApiParam(name = "userId", value = "推广员Id")@RequestParam(required = false) Integer  userId,
                               @ApiParam(name = "applyId", value = "申请表Id")@RequestParam(required = false) String  applyId,
                               @ApiParam(name = "companyId", value = "公司Id")@RequestParam(required = false) Integer  companyId,
                                HttpServletRequest request) {
        try {
            scUserService.applyPromoter(userId,applyId,companyId,request);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序用户登录")
    @PostMapping("/userLogin")
    public Result userLogin(@ApiParam(name = "code", value = "code值") @RequestParam("code")  String  code,
                            @ApiParam(name = "companyId", value = "上级推广员绑定的companyId,如果是直接搜小程序来的，则传1") @RequestParam("companyId") Integer companyId,
                                        @ApiParam(name = "parentId", value = "推广员id") @RequestParam("parentId") Integer parentId,
                            @ApiParam(name = "shareId", value = "推广用户id") @RequestParam("shareId") Integer shareId){
        try {
            Map map  =scUserService.userLogin(code,companyId,parentId,shareId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序我的推广会员，邀请码以及推广员")
    @PostMapping("/listMyUser")
    public Result listMyUser(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId")  Integer  userId,
                             @ApiParam(name = "flag", value = "0邀请码 1邀请人 2我的推广会员") @RequestParam("flag")  Integer flag,
                             HttpServletRequest request){
        try {
            Map map  =scUserService.listMyUser(userId,flag,request);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("小程序用户申请成为推广员")
    @PostMapping("/addApply")
    public Result addApply(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId")  Integer  userId,
                             @ApiParam(name = "companyId", value = "推广公司") @RequestParam("companyId")  Integer  companyId){
        try {
            scUserService.addApply(userId,companyId);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("小程序我的佣金")
    @PostMapping("/listMyCommission")
    public Result listMyCommission(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId")  Integer  userId,
                           @ApiParam(name = "companyId", value = "推广公司") @RequestParam("companyId")  Integer  companyId){
        try {
            Map map = scUserService.listMyCommission(userId,companyId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序修改我的信息")
    @PostMapping("/updateUser")
    public Result updateUser(Integer userId,String userName,String userPhone,HttpServletRequest request){
        try {
            SUser user = scUserService.updateUser(userId,userName,userPhone,request);
            return new Result(true, StatusCode.OK,"查询成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("小程序获取我的资产详情")
    @PostMapping("/listMyAccount")
    public Result listMyAccount(Integer userId){
        try {
            Map map  = scUserService.listMyAccount(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("小程序推广码兑换身份")
    @PostMapping("/exchangeUserType")
    public Result exchangeUserType(Integer userId,String userName,String userPhone,String code){
        try {
            Map map  = scUserService.exchangeUserType(userId,userName,userPhone,code);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序删除地址")
    @PostMapping("/deleteAddress")
    public Result deleteAddress(Integer addressId){
        try {
            scUserService.deleteAddress(addressId);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }

    @ApiOperation("小程序设置默认地址")
    @PostMapping("/setAddress")
    public Result setAddress(Integer addressId,Integer userId){
        try {
            scUserService.setAddress(addressId,userId);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation("推广员离职申请")
    @PostMapping("/PromoterDown")
    public Result PromoterDown(Integer userId,Integer companyId,String note){
        try {
            scUserService.PromoterDown(userId,companyId,note);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("推广员离职通过")
    @PostMapping("/PromoterDownApply")
    public Result PromoterDownApply(@ApiParam(name = "userId", value = "推广员Id")@RequestParam(required = false) Integer  userId,
                                    @ApiParam(name = "applyId", value = "申请表Id")@RequestParam(required = false) String  applyId,
                                    @ApiParam(name = "companyId", value = "公司Id")@RequestParam(required = false) Integer  companyId){
        try {
            scUserService.PromoterDownApply(userId,applyId,companyId);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序获取邀请会员进度")
    @PostMapping("/listMemberNum")
    public Result listMemberNum(Integer userId){
        try {
           Object num =  scUserService.listMemberNum(userId);
            return new Result(true, StatusCode.OK,"查询成功",num);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序提现")
    @PostMapping("/getMoney")
    public Result getMoney(Integer userId,String openId,String userName,String money){
        try {
            Map map = scUserService.getMoney(userId, openId, userName,money);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序获取最新用户信息")
    @PostMapping("/newGetUser")
    public Result newGetUser(Integer userId){
        try {
            SUser user = scUserService.newGetUser(userId);
            return new Result(true, StatusCode.OK,"查询成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


}
